package com.iCall.sip;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SipServer implements Runnable{
	
	private boolean running = true;
	
	private ServerSocket serverSock;
	
	private int port;
	
	public SipServer(int port) {
		this.port = port;
	}
	
	public void start() throws IOException{
		serverSock = new ServerSocket(port);
		new Thread(this, "SipServer-" + port).start();
	}
	
	public void run() {
		while (running){
			try {
				Socket sock = null;
				try {
					sock = serverSock.accept();
				} catch (IOException e) {
					System.out.println("something error");
					break;
				}
				
				InputStream in = sock.getInputStream();
				OutputStream out = sock.getOutputStream();
				byte[] bytes = new byte[4028];
				int len = 0;
				while ((len = in.read(bytes)) > 0){
					out.write(bytes, 0, len);
				}
			} catch (Exception e) {
				
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		new SipServer(9001).start();
	}
}
